//登录模块，用户数据账户

import { defineStore } from 'pinia'
import { ref, watch, onMounted } from 'vue'

export const useLoginStore = defineStore('login', () => {
  const user = ref({})

  watch(
    user,
    () => {
      localStorage.setItem('account', JSON.stringify(user.value))
    },
    {
      deep: true
    }
  )
  const setUser = (val) => {
    user.value = val
  }

  // 本地账号保存
  onMounted(() => {
    const storeAccount = localStorage.getItem('account')
    if (storeAccount) {
      user.value = JSON.parse(storeAccount)
    }
  })
  return {
    user,
    setUser
  }
})
